<template>
  <el-dialog
    :title="dialogType === 'new' ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="flagVisible"
    width="70%"
    :before-close="handleClose"
  >
    <OrganForm
      ref="organForm"
      :formData="formData"
      :formItme="formItme"
      :formHandler="form_handler"
    />
  </el-dialog>
</template>

<script>
import OrganForm from "@/components/CommonForm/form-item/OrganForm.vue"
// 加载模拟数据
import { changeType } from "@/simdata/healthData.js"
export default {
  components: {
    OrganForm
  },
  props: {
    formData: {
      type: Object,
      default: () => {}
    },
    formItme: {
      type: Array,
      default: () => []
    },
    flagVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: "new"
    }
  },
  data() {
    return {
      // 弹窗显示/关闭标记
      // dialogVisible: false,
      // 表单按钮
      form_handler: [
        { label: "取消", type: "", handler: () => this.cancel() },
        { label: "确定", type: "primary", handler: () => this.submit() }
      ]
    }
  },
  created() {},
  methods: {
    // 重置表单
    reset(formName) {
      Object.assign(
        this.$data.form_data,
        this.$options.data.call(this).form_data
      )
      console.log(this.form_data)
    },
    // 关闭弹框
    handleClose() {
      // this.reset("form") // 重置表单
      this.$refs.organForm.resetForm()
      // this.dialogVisible = false
      this.$emit("update:flagVisible", false)
      this.$emit("update:dialogType", "new")
      this.$emit("clearCheckboxRowSelect", "清空选中")
    },
    // 取消按钮
    cancel() {
      this.handleClose()
    },
    // 确认提交
    submit() {
      console.log("点击提交按钮了")
      this.handleAdd()
    },
    // 添加
    handleAdd() {
      this.$refs.organForm.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.formData)
          this.$emit("submitData", this.formData)
          // this.handleClose()
        } else {
          console.log("error submit!!")
          return false
        }
      })
    }
  }
  // watch: {
  //   flagVisible: {
  //     handler(newValue, oldValue) {
  //       this.dialogVisible = newValue
  //     }
  //   }
  // }
}
</script>

<style></style>
